package nl.weeaboo.sh;

import nl.weeaboo.game.input.UserInput;

public class ComboCollector {

	private static final float threshold = 0.2f;

	private int comboKeys[];
	private boolean wasPressed[];
	
	private int pressed[];
	private int pressedIndex;
	private float time;
	
	public ComboCollector(int comboKeys[]) {
		this.comboKeys = comboKeys;
		
		wasPressed = new boolean[comboKeys.length];

		pressed = new int[4];		
		pressedIndex = 0;
		time = 0;
	}
	
	//Functions
	public void update(UserInput input, float dt) {
		time += dt;

		for (int n = 0; n < comboKeys.length; n++) {
			boolean p = input.isKeyPressed(comboKeys[n]);
			if (p && !wasPressed[n]) {
				if (pressedIndex >= pressed.length) {
					for (int x = 1; x < pressedIndex; x++) {
						pressed[x-1] = pressed[x];
					}
					pressedIndex--;
				}
				pressed[pressedIndex++] = comboKeys[n];
				time = 0;
				
				/*for (int x = 0; x < pressedIndex; x++) {
					System.out.print(pressed[x] + " ");
				}
				System.out.println();*/
			}
			wasPressed[n] = p;
		}
				
		if (time >= threshold) {
			pressedIndex = 0;
			time = 0;
		}
	}
	
	//Getters
	public boolean consumeCombo(int keys[]) {
		for (int n = 0; n <= pressedIndex - keys.length; n++) {
			int x;
			for (x = 0; x < keys.length; x++) {
				if (keys[x] != pressed[n+x]) {
					break;
				}
			}
			if (x == keys.length) {
				System.arraycopy(pressed, n+x, pressed, 0, pressedIndex-(n+x));
				pressedIndex -= n+x;
				return true;
			}
		}
		return false;
	}
	
	public int[] getPressed() { return pressed; }
	public int getPressedLength() { return pressedIndex; }
	
	//Setters
	
}
